home *** CD-ROM | disk | FTP | other *** search
/ Power CD-ROM!! 8 / Power CD-ROM 8.iso / prgmming / oldutil / unique.asm < prev    next >
Encoding:
Assembly Source File  |  1994-12-10  |  3.5 KB  |  159 lines

  1.  
  2.     page ,132
  3.     title Unique - a UNIX-like filter
  4.  
  5. STDIN    equ    0
  6. STDOUT    equ    1
  7. STDERR    equ    2
  8. CR    equ    0dh
  9. LF    equ    0ah
  10. CPMEOF    equ    1ah
  11.  
  12. Cseg    Segment
  13.     Assume    CS:Cseg,DS:Cseg,ES:Cseg,SS:Cseg
  14.  
  15.     Org    80h
  16. Inline    Label    Byte
  17.     db    128 Dup (?)
  18.  
  19. Unique    Proc    Far
  20.     jmp    Start
  21.  
  22. ; message text and data area
  23. Header    db    CR,LF,'Unique - Version 1.0 - 24 Feb 1985'
  24.     db    CR,LF,' by Ray L. McVay'
  25.     db    CR,LF,'    908 Newton'
  26.     db    CR,LF,'    Arlington, TX 76010'
  27.     db    CR,LF,'    CompuServe [75006,2563]',CR,LF,CPMEOF
  28.  
  29. Inlen    dw    0
  30. Outlen    dw    0
  31.     
  32. Start:
  33.     mov    AX,offset Outline
  34.     add    AX,256
  35.     mov    SP,AX        ; We're going to shrink up later.
  36.     mov    AH,30h        ;get DOS version
  37.     int    21h
  38.     cmp    AL,2
  39.     jae    VerOK
  40.     int    20h        ; old exit
  41.  
  42. VerOK:
  43.     mov    BX,SP            ; SP = top of needed ram
  44.     mov    CL,4
  45.     sar    BX,CL            ; Divide memory used by 16.
  46.     inc    BX            ; Round up 1 paragraph (CYA).
  47.     mov    AH,4ah            ; SETBLOCK
  48.     int    21h            ; Assume it worked.
  49.  
  50.     mov    BX,STDIN        ; get the first line from STDIN
  51.     mov    DX,offset Inline
  52.     call    Fgets
  53.     mov    Inlen,AX
  54.     jnz    Uloop
  55.     mov    AX,4c01h
  56.     int    21h            ; exit(1)
  57.  
  58. Uloop:
  59.     mov    CX,Inlen        ; Copy Inline to Outline
  60.     mov    Outlen,CX        ; first copy the length
  61.     inc    CX            ; so the EOS will be copied
  62.     mov    SI,offset Inline    ; then the line
  63.     mov    DI,offset Outline
  64.     cld
  65.     rep    movsb
  66.  
  67. Nextline:
  68.     mov    BX, STDIN
  69.     mov    DX,offset Inline
  70.     call    Fgets            ; Get next line from STDIN
  71.     mov    Inlen,AX
  72.                     ; Now compare Inline to Outline
  73.     cmp    Outlen,AX        ; Unequal lengths can't match!
  74.     jne    NotSame
  75.     mov    CX,AX            ; The lengths are equal so we'll
  76.     mov    SI,offset Inline    ; compare the strings.
  77.     mov    DI,offset Outline
  78.     repe    cmpsb
  79.     or    CX,CX            ; If CX=0 then Inline=Outline
  80.     je    Nextline
  81.  
  82. NotSame:
  83.     mov    BX,STDOUT
  84.     mov    DX,offset Outline
  85.     call    Fputs
  86.     test    Inlen,-1        ; Did we hit end of file?
  87.     jnz    Uloop            ; No. Get next line.
  88.  
  89.     mov    AX,4c00h
  90.     int    21h            ; exit(0)
  91.  
  92. ; Get a linefeed terminated string into the buffer pointed to
  93. ; by DS:DX from the file whose handle is in BX.  Make it an ASCIIZ
  94. ; string and return the length in AX.  Return 0 (and ZF) on End of File.
  95.  
  96. Fgets    Proc    Near
  97.  
  98.     push    SI        ; Save the working registers.
  99.     push    CX
  100.     push    DX
  101.     mov    SI,DX        ; For easy addressability in the loop.
  102. NextChar:
  103.     mov    CX,1        ; Read one character at a time.
  104.     mov    AH,3fh
  105.     int    21h
  106.     dec    AX        ; AX should be 1 so the dec will make
  107.     jnz    HitEnd        ; it 0 unless end of file was found.
  108.     cmp    byte ptr [SI],LF
  109.     je    GotLF
  110.     inc    DX
  111.     inc    SI
  112.     jmp    NextChar
  113. GotLF:
  114.     inc    SI        ; Point to byte following the LF.
  115.     mov    byte ptr [SI],0    ; Make it EOS ala UNIX.
  116.     pop    DX
  117.     sub    SI,DX        ; length = start of buffer - SI
  118.     mov    AX,SI
  119.     jmp    short    XFgets
  120. HitEnd:
  121.     sub    AX,AX        ; Discard last line if it's a partial
  122.     pop    DX
  123. XFgets:
  124.     pop    CX        ; Restore working registers
  125.     pop    SI
  126.     ret
  127. Fgets    endp
  128.  
  129. ; Send an ASCIIZ string pointed to by DX to the file whose
  130. ; handle is in BX.  Returns status in AX.
  131.  
  132. Fputs    Proc    Near
  133.  
  134.     push    CX
  135.     push    DI
  136.     mov    DI,DX        ; Calculate the length of the string
  137.     sub    AX,AX        ; by scanning for the null at the end.
  138.     mov    CX,-1        ; Set maximum repeat count.
  139.     repne    scasb        ; DI will then point 1 byte past EOS null
  140.     sub    DI,DX        ; so DI-DX-1 should equal the length.
  141.     mov    CX,DI
  142.     dec    CX
  143.     mov    AH,40h
  144.     int    21h
  145.     pop    DI
  146.     pop    CX
  147.     ret
  148. Fputs    endp
  149.  
  150. Unique    endp
  151.  
  152. ; Output line buffer is placed here so it won't
  153. ; eat up a lot of space in the COM file.
  154.  
  155. Outline    label byte        ; 128 byte output line buffer.
  156.                 ; 128 byte local stack follows.
  157. Cseg    ends
  158.     end    Unique
  159.